<?php
class User_MediaController extends Zend_Controller_Action
{
	private $model;
	private $auth;
	public function init()
	{
		$this->model = new User_Model_Basic();
		$this->auth = Zend_Auth::getInstance();
		$dirfile = $this->_request->getBaseUrl() . "/application/layouts/user";
		$dirlayout = APPLICATION_PATH . "/layouts/user";
		$this->view->imgs = $dirfile . "/images";
		$this->view->headLink()->appendStylesheet($dirfile . '/css/style.css');
		$this->view->headLink()->appendStylesheet($dirfile . '/css/jquery-ui-1.8.18.custom.css');
		$this->view->headLink()->appendStylesheet($dirfile . '/upload/uploadify.css');
		$this->view->headScript()->appendFile($dirfile . '/js/jquery-1.7.2.min.js');
		$this->view->headScript()->appendFile($dirfile . '/js/jquery-ui-1.8.18.custom.min.js');
		$this->view->headScript()->appendFile($dirfile . '/js/dropdown.js');
		$this->view->headScript()->appendFile($dirfile . '/js/slidedown.js');
		$this->view->headScript()->appendFile($dirfile . '/js/datepick2.js');
		$this->view->headScript()->appendFile($dirfile . '/upload/jquery.uploadify-3.1.min.js');
		$this->view->headScript()->appendFile($dirfile . '/upload/upyoutb.js');
		$this->view->headScript()->appendFile($dirfile . '/upload/uploadimg.js');
		$this->view->headScript()->appendFile($dirfile . '/upload/uploadvideo.js');
		$this->view->headScript()->appendFile($dirfile . '/js/getCategory.js');
		$option = array('layoutPath'=>$dirlayout,'layout'=>'index');
		Zend_Layout::startMvc($option);
	}
	public function indexAction()
	{
		$data_catelog = $this->model->getallnamecatelog();
		$this->view->category = $data_catelog;
		$this->view->id_user = $this->auth->getIdentity()->id_user;
	}
	public function getcategoryAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$select = $this->_request->getParam('select');
		$id_category = $this->model->getidcatelog($select);
		$data = $this->model->getproduct($id_category['id_catelog']);
		echo Zend_Json::encode($data);
	}
	public function uploadfileimgAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		if (!empty($_FILES)) {
			$upload = new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination(APPLICATION_PATH.'/../public');
			$fileinfo = $upload->getFileInfo();
			try {
				$upload->receive();
				if ($upload->isUploaded())
				{
					/* foreach ($fileinfo as $file=>$tempinfo)
						{
					$name_url = $this->_request->getBaseUrl() . '/public/' . $tempinfo['name'];
					$insert= $this->model->addimg($name_url, $tempinfo['name'], $tempinfo['size'],$id_product['id_product']);
					} */
					$img = new Zend_Session_Namespace('imageuser');
					$img->fileinfo = $fileinfo;
				}
			} catch (Exception $e) {
				echo $e->getMessage();
			}
		}
	}
	public function upyoutubeAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$category = $this->_request->getParam('category');
		$product = $this->_request->getParam('product');
		$path = $this->_request->getParam('path');
		$id_product = $this->model->getidproduct($product);
		$name_video = $this->_request->getParam('name');
		$date = $this->_request->getParam('date');
		$user = $this->_request->getParam('id_user');
		//$user = $this->auth->getIdentity()->id_user;
		if (!empty($_FILES)) {
			$upload = new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination(APPLICATION_PATH.'/../public');
			$fileinfo = $upload->getFileInfo();
			try {
				$upload->receive();
				if ($upload->isUploaded())
				{
					foreach ($fileinfo as $file=>$tempinfo)
					{
						$name_img = $this->_request->getBaseUrl() . '/public/' . $tempinfo['name'];
						$insert= $this->model->addvideoytb($name_video, $date, $path,$tempinfo['name'],$name_img,$tempinfo['size'], $id_product['id_product'], $user, 1);
						echo "Upload Success";
					}
				}
			}catch(Zend_Db_Exception $e)
			{
				echo "Loi xu ly";
			}
		}
	}
	public function uploadfilevideoAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$category = $this->_request->getParam('category');
		$product = $this->_request->getParam('product');
		//$path = $this->_request->getParam('path');
		$id_product = $this->model->getidproduct($product);
		$name_video = $this->_request->getParam('name');
		$date = $this->_request->getParam('date');
		$id_user = $this->_request->getParam('id_user');
		$img = new Zend_Session_Namespace('imageuser');
		$fileimg = $img->fileinfo;
		if (!empty($_FILES)) {
			$upload = new Zend_File_Transfer_Adapter_Http();
			$upload->setDestination(APPLICATION_PATH.'/../public');
			$fileinfo = $upload->getFileInfo();
			try {
				$upload->receive();
				if ($upload->isUploaded())
				{
					foreach ($fileinfo as $file=>$tempinfo)
					{
						$name_url = $this->_request->getBaseUrl() . '/public/' . $tempinfo['name'];
						$name_img = $this->_request->getBaseUrl() . '/public/' . $fileimg['file']['name'];
						$insert= $this->model->addvideo($name_video, $date, $name_url, $tempinfo['size'],$fileimg['file']['name'],$name_img,$fileimg['file']['size'], $id_product['id_product'], $id_user, 2);
					}
				}
			} catch (Exception $e) {
				echo $e->getMessage();
			}
	
		}
	
	}
	public function manageAction()
	{
		$id_user = $this->auth->getIdentity()->id_user;
		$data = $this->model->getallvideo($id_user);
		$paging = Zend_Paginator::factory($data);
		$paging->setItemCountPerPage(2);
		$paging->setPageRange(2);
		$current = $this->_request->getParam('page',1);
		$paging->setCurrentPageNumber($current);
		$this->view->paging = $paging;
	}
	public function getsearchAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		$content = $this->_request->getParam('text-search');
		$session = new Zend_Session_Namespace('search');
		$session->content = $content;
		$this->_forward('search');
	
	}
	public function searchAction()
	{
		$id_user = $this->auth->getIdentity()->id_user;
		$session = new Zend_Session_Namespace('search');
		$content = $session->content;
		$data = $this->model->search($id_user,$content);
		$paging = Zend_Paginator::factory($data);
		$paging->setItemCountPerPage(2);
		$paging->setPageRange(2);
		$current = $this->_request->getParam('page',1);
		$paging->setCurrentPageNumber($current);
		$this->view->pagingsearch = $paging;
	}
}